<template>
  <div>
    <Header scroll placeholder></Header>
    <div class="banner-section">
      <div class="banner-container">
        <img src="@/assets/about/gywm_banner.png" alt="供链合作" class="banner-img" :class="{ 'mobile-banner': isMobile }" />
        <div class="banner-overlay">
          <div class="banner-content">
            <h2 class="banner-title">供链合作</h2>
            <p class="banner-subtitle">SUPPLY CHAIN COOPERATION</p>
          </div>
        </div>
      </div>
      <div class="banner-button-section">
        <div class="btn banner-btn">供链合作</div>
      </div>
    </div>
    <!-- 表单区域 -->
    <div class="form-section">
      <!-- 桌面端表单 -->
      <div v-if="!isMobile" class="desktop-form-container">
        <div class="form-wrapper">
          <div class="form-header">
            <h3 class="form-title">供链合作申请表</h3>
            <p class="form-description">请填写以下信息，我们将尽快与您联系</p>
          </div>

          <el-form ref="formRulesRef" :model="formData" :rules="formRules" label-width="90px" class="desktop-form">
            <el-form-item label="姓名: " prop="contact_name">
              <el-input v-model="formData.contact_name" size="large" placeholder="必填" clearable></el-input>
            </el-form-item>

            <el-form-item label="电话: " prop="contact_tel">
              <el-input v-model="formData.contact_tel" size="large" placeholder="必填" clearable></el-input>
            </el-form-item>

            <el-form-item label="合作详情: " prop="contact_text">
              <el-input v-model="formData.contact_text" size="large" :rows="2" type="textarea" placeholder="必填" clearable></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="submitForm" class="submit-btn">提交申请</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>

      <!-- 移动端表单 -->
      <div v-else class="mobile-form-container">
        <div class="mobile-form-wrapper">
          <div class="mobile-form-header">
            <h3 class="mobile-form-title">供链合作申请</h3>
            <p class="mobile-form-description">请填写合作信息</p>
          </div>

          <el-form ref="formRulesRef" :model="formData" :rules="formRules" label-width="90px">
            <el-form-item label="姓名: " prop="contact_name">
              <el-input v-model="formData.contact_name" size="large" placeholder="必填" clearable></el-input>
            </el-form-item>

            <el-form-item label="电话: " prop="contact_tel">
              <el-input v-model="formData.contact_tel" size="large" placeholder="必填" clearable></el-input>
            </el-form-item>

            <el-form-item label="合作详情: " prop="contact_text">
              <el-input v-model="formData.contact_text" size="large" :rows="2" type="textarea" placeholder="必填" clearable></el-input>
            </el-form-item>

            <!-- 提交按钮 -->
            <div class="mobile-form-actions">
              <button type="button" class="mobile-submit-btn" @click="submitForm">提交合作申请</button>
            </div>
          </el-form>
        </div>
      </div>
    </div>

    <Footer></Footer>
    <SettleIn />
  </div>
</template>

<script setup>
  import { ref, onMounted, onUnmounted, computed } from 'vue'
  import { ElMessage } from 'element-plus'
  import Header from '@/components/Header.vue'
  import Footer from '@/components/Footer.vue'
  import SettleIn from '@/components/SettleIn.vue'
  import apiGood from '@/api/modules/good'

  const isMobile = ref(false)
  const deviceInfo = ref({
    type: '',
    os: '',
    browser: '',
    screenWidth: 0,
  })

  // 表单数据
  const formData = ref({
    form_type: 3, // 类型：1-茶叶定制 2-招商加盟 3-供应链合作 4-其他
    contact_name: '',
    contact_tel: '',
    city: '',
    investment: '',
    experience: '',
    storeType: '',
    contact_images: [],
    contact_text: '',
  })

  // 表单验证规则
  const formRulesRef = ref()
  const formRules = {
    contact_name: [
      { required: true, message: '请输入收货人姓名', trigger: 'blur' },
      { min: 2, max: 10, message: '姓名长度在 2 到 10 个字符', trigger: 'blur' },
    ],
    contact_tel: [
      { required: true, message: '请输入手机号码', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' },
    ],
    contact_text: [{ required: true, message: '请输入您的产品服务等信息', trigger: 'blur' }],
  }

  // 提交表单
  const submitForm = () => {
    formRulesRef.value &&
      formRulesRef.value.validate((valid) => {
        if (valid) {
          // 这里可以添加实际的提交逻辑
          apiGood.AddContact(formData.value).then(async (res) => {
            ElMessage.success('提交成功！我们会尽快联系您')
            // 重置表单
            formData.value = {
              form_type: 3, // 类型：1-茶叶定制 2-招商加盟 3-供应链合作 4-其他
              contact_name: '',
              contact_tel: '',
              city: '',
              investment: '',
              experience: '',
              storeType: '',
              contact_images: [],
              contact_text: '',
            }
          })
        }
      })
  }

  // 获取设备信息
  const getDeviceInfo = () => {
    const userAgent = navigator.userAgent.toLowerCase()
    const screenWidth = window.innerWidth

    // 检测操作系统
    let os = 'Unknown'
    if (userAgent.includes('android')) os = 'Android'
    else if (userAgent.includes('iphone') || userAgent.includes('ipad')) os = 'iOS'
    else if (userAgent.includes('windows')) os = 'Windows'
    else if (userAgent.includes('mac')) os = 'macOS'
    else if (userAgent.includes('linux')) os = 'Linux'

    // 检测浏览器
    let browser = 'Unknown'
    if (userAgent.includes('chrome')) browser = 'Chrome'
    else if (userAgent.includes('firefox')) browser = 'Firefox'
    else if (userAgent.includes('safari')) browser = 'Safari'
    else if (userAgent.includes('edge')) browser = 'Edge'

    // 检测设备类型
    let type = 'Desktop'
    if (userAgent.includes('mobile') || screenWidth <= 768) type = 'Mobile'
    else if (userAgent.includes('tablet') || (screenWidth > 768 && screenWidth <= 1024)) type = 'Tablet'

    deviceInfo.value = { type, os, browser, screenWidth }

    return { type, os, browser, screenWidth }
  }

  // 检测是否为移动设备
  const checkMobile = () => {
    const { type, screenWidth } = getDeviceInfo()
    const userAgent = navigator.userAgent.toLowerCase()

    // 更精确的移动设备检测
    const mobileKeywords = ['mobile', 'android', 'iphone', 'ipod', 'blackberry', 'windows phone']
    const tabletKeywords = ['ipad', 'tablet']

    const isMobileDevice = mobileKeywords.some((keyword) => userAgent.includes(keyword))
    const isTabletDevice = tabletKeywords.some((keyword) => userAgent.includes(keyword))
    const isSmallScreen = screenWidth <= 768
    const isMediumScreen = screenWidth > 768 && screenWidth <= 1024

    // 移动设备或小屏幕显示移动端界面
    isMobile.value = isMobileDevice || isSmallScreen || (isTabletDevice && isMediumScreen)
  }

  // 监听窗口大小变化
  const handleResize = () => {
    checkMobile()
  }

  // 组件挂载时初始化
  onMounted(() => {
    checkMobile()
    window.addEventListener('resize', handleResize)
  })

  onUnmounted(() => {
    window.removeEventListener('resize', handleResize)
  })
</script>

<style scoped>
  /* 基础样式 */
  .btn {
    background-color: #1f7260;
    border: 1px solid white;
    padding: 10px 30px;
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    border-radius: 4px;
  }

  .btn:hover {
    background-color: #155a4a;
    transform: translateY(-2px);
  }

  /* 横幅区域样式 */
  .banner-section {
    position: relative;
  }

  .banner-container {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .banner-img {
    width: 100%;
    height: 350px;
    object-fit: cover;
  }

  .mobile-banner {
    height: 250px;
  }

  .banner-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.3);
  }

  .banner-content {
    text-align: center;
    color: white;
  }

  .banner-title {
    font-size: 2.5rem;
    font-weight: bold;
    margin: 0;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
  }

  .banner-subtitle {
    font-size: 1.2rem;
    margin: 10px 0;
    color: rgba(255, 255, 255, 0.8);
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
  }

  .banner-button-section {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 1rem 0;
  }

  .banner-btn {
    font-size: 1rem;
    padding: 12px 40px;
  }

  /* 表单区域样式 */
  .form-section {
    background: url('@/assets/contact/lxwm_bj.png');
    background-size: 100% 100%;
    background-position: center;
    background-repeat: no-repeat;
    min-height: 100vh;
    padding: 40px 0;
  }

  /* 桌面端表单样式 */
  .desktop-form-container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 2rem;
  }

  .form-wrapper {
    max-width: 900px;
    width: 100%;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    padding: 40px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
  }

  .form-header {
    text-align: center;
    margin-bottom: 30px;
  }

  .form-title {
    color: #1f7260;
    font-size: 2rem;
    font-weight: bold;
    margin: 0 0 10px 0;
  }

  .form-description {
    color: #666;
    font-size: 1rem;
    margin: 0;
  }

  .desktop-form {
    margin-top: 20px;
  }

  .radio-group-vertical {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  .radio-group-horizontal {
    display: flex;
    gap: 20px;
  }

  .radio-item {
    margin-bottom: 10px !important;
    margin-right: 0 !important;
  }

  .full-width {
    width: 100%;
  }

  .submit-btn {
    background: linear-gradient(135deg, #1f7260 0%, #27ae60 100%);
    border: none;
    padding: 14px 40px;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    color: white;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(31, 114, 96, 0.3);
  }

  .submit-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(31, 114, 96, 0.4);
  }

  /* 移动端表单样式 */
  .mobile-form-container {
    padding: 20px;
    min-height: 100vh;
    display: flex;
    align-items: flex-start;
    justify-content: center;
  }

  .mobile-form-wrapper {
    width: 100%;
    max-width: 500px;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    padding: 25px 20px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
    backdrop-filter: blur(10px);
  }

  .mobile-form-header {
    text-align: center;
    margin-bottom: 25px;
  }

  .mobile-form-title {
    color: #1f7260;
    font-size: 1.5rem;
    font-weight: bold;
    margin: 0 0 8px 0;
  }

  .mobile-form-description {
    color: #666;
    font-size: 0.9rem;
    margin: 0 0 20px 0;
  }

  /* 进度指示器样式 */
  .progress-indicator {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 10px;
    padding: 15px 0;
  }

  .progress-step {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    transition: all 0.3s ease;
  }

  .step-number {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: #e0e0e0;
    color: #999;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 600;
    transition: all 0.3s ease;
  }

  .step-label {
    font-size: 10px;
    color: #999;
    text-align: center;
    transition: all 0.3s ease;
  }

  .progress-step.active .step-number {
    background: #1f7260;
    color: white;
  }

  .progress-step.active .step-label {
    color: #1f7260;
    font-weight: 600;
  }

  .progress-line {
    width: 40px;
    height: 2px;
    background: #e0e0e0;
    margin: 0 10px;
    transition: all 0.3s ease;
  }

  .progress-line.active {
    background: #1f7260;
  }

  .mobile-form {
    display: flex;
    flex-direction: column;
    gap: 25px;
  }

  .mobile-form-section {
    background: rgba(248, 249, 250, 0.8);
    border-radius: 12px;
    padding: 20px;
    border: 1px solid rgba(31, 114, 96, 0.1);
  }

  .section-title {
    color: #1f7260;
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0 0 8px 0;
    padding-bottom: 8px;
    border-bottom: 2px solid rgba(31, 114, 96, 0.2);
  }

  .section-hint {
    color: #999;
    font-size: 12px;
    margin: 0 0 15px 0;
    line-height: 1.4;
  }

  .mobile-radio-group {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .mobile-radio-item {
    background: white;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    padding: 12px 16px;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 14px;
    text-align: center;
  }

  .mobile-radio-item:hover {
    border-color: #1f7260;
    background: rgba(31, 114, 96, 0.05);
  }

  .mobile-radio-item.active {
    border-color: #1f7260;
    background: rgba(31, 114, 96, 0.1);
    color: #1f7260;
    font-weight: 600;
  }

  .mobile-input-group,
  .mobile-select-group {
    margin-bottom: 15px;
  }

  .mobile-input,
  .mobile-select,
  .mobile-textarea {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid #e0e0e0;
    border-radius: 8px;
    font-size: 14px;
    transition: all 0.3s ease;
    background: white;
    color: #333;
  }

  .mobile-input:focus,
  .mobile-select:focus,
  .mobile-textarea:focus {
    outline: none;
    border-color: #1f7260;
    box-shadow: 0 0 0 3px rgba(31, 114, 96, 0.1);
  }

  .mobile-input.error,
  .mobile-select.error,
  .mobile-textarea.error {
    border-color: #e74c3c;
    background: rgba(255, 235, 235, 0.5);
  }

  .mobile-input::placeholder,
  .mobile-select::placeholder,
  .mobile-textarea::placeholder {
    color: #999;
  }

  .mobile-textarea {
    resize: vertical;
    min-height: 80px;
    font-family: inherit;
  }

  .textarea-wrapper {
    position: relative;
  }

  .char-counter {
    position: absolute;
    bottom: 8px;
    right: 12px;
    font-size: 11px;
    color: #999;
    background: rgba(255, 255, 255, 0.9);
    padding: 2px 6px;
    border-radius: 4px;
    pointer-events: none;
  }

  .mobile-error {
    color: #e74c3c;
    font-size: 12px;
    margin-top: 5px;
    display: block;
  }

  .mobile-form-actions {
    margin-top: 10px;
  }

  .mobile-submit-btn {
    width: 100%;
    background: linear-gradient(135deg, #1f7260 0%, #27ae60 100%);
    color: white;
    border: none;
    padding: 16px 20px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 15px rgba(31, 114, 96, 0.3);
  }

  .mobile-submit-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(31, 114, 96, 0.4);
  }

  .mobile-submit-btn:active {
    transform: translateY(0);
  }

  .mobile-submit-btn.btn-ready {
    background: linear-gradient(135deg, #27ae60 0%, #2ecc71 100%);
    box-shadow: 0 4px 15px rgba(39, 174, 96, 0.4);
  }

  .mobile-submit-btn.btn-ready:hover {
    box-shadow: 0 8px 25px rgba(39, 174, 96, 0.5);
  }

  /* 表单完成度样式 */
  .form-completeness {
    background: rgba(248, 249, 250, 0.8);
    border-radius: 12px;
    padding: 15px;
    margin-bottom: 20px;
    border: 1px solid rgba(31, 114, 96, 0.1);
  }

  .completeness-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
  }

  .completeness-label {
    font-size: 14px;
    color: #666;
    font-weight: 500;
  }

  .completeness-percentage {
    font-size: 14px;
    color: #1f7260;
    font-weight: 600;
  }

  .completeness-bar {
    width: 100%;
    height: 6px;
    background: #e0e0e0;
    border-radius: 3px;
    overflow: hidden;
  }

  .completeness-fill {
    height: 100%;
    background: linear-gradient(90deg, #1f7260 0%, #27ae60 100%);
    border-radius: 3px;
    transition: width 0.3s ease;
  }

  /* 响应式设计 */
  @media (max-width: 768px) {
    .banner-title {
      font-size: 2rem;
    }

    .banner-subtitle {
      font-size: 1rem;
      margin: 8px 0;
    }

    .banner-btn {
      padding: 10px 30px;
      font-size: 0.9rem;
    }

    .form-section {
      padding: 20px 0;
    }

    .mobile-form-wrapper {
      padding: 20px 15px;
    }

    .mobile-form-section {
      padding: 15px;
    }

    .section-title {
      font-size: 1rem;
    }

    .mobile-radio-item {
      padding: 10px 12px;
      font-size: 13px;
    }

    .mobile-input,
    .mobile-select,
    .mobile-textarea {
      padding: 10px 12px;
      font-size: 13px;
    }
  }

  @media (max-width: 480px) {
    .banner-img {
      height: 200px;
    }

    .banner-title {
      font-size: 1.8rem;
    }

    .banner-subtitle {
      font-size: 0.9rem;
    }

    .banner-btn {
      padding: 8px 20px;
      font-size: 0.8rem;
    }

    .mobile-form-wrapper {
      padding: 15px 10px;
      margin: 0 10px;
    }

    .mobile-form-title {
      font-size: 1.3rem;
    }

    .mobile-form-description {
      font-size: 0.8rem;
    }

    .mobile-form {
      gap: 20px;
    }

    .mobile-form-section {
      padding: 12px;
    }

    .section-title {
      font-size: 0.9rem;
      margin-bottom: 12px;
    }

    .mobile-radio-group {
      gap: 8px;
    }

    .mobile-radio-item {
      padding: 8px 10px;
      font-size: 12px;
    }

    .mobile-input,
    .mobile-select,
    .mobile-textarea {
      padding: 8px 10px;
      font-size: 12px;
    }

    .mobile-submit-btn {
      padding: 14px 16px;
      font-size: 14px;
    }
  }
</style>
